# coding=utf-8
from django.conf import settings
from sutras import enums, models
from sutras.enums import (
    SutraStatus, UserTranscription, SutraEditionStatus)


def sutras_edition_search(user, name, offset, limit):
    result = models.SutraEdition.objects.filter(
        name__contains=name,
        status=enums.SutraEditionStatus.normal)[offset: offset + limit]
    finisheds = set(models.UserTranscription.objects.filter(
        user=user,
        status=UserTranscription.finished).values_list(
        'sutra_edition', flat=True))
    starreds = set(models.UserFavorite.objects.filter(
        user=user).values_list('sutra_edition_id', flat=True))
    data = []
    for res in result:
        cover_url = "{0}{1}".format(
            settings.SELF_DOMAIN, res.cover_url)
        data.append({
            'sutra_id': res.sutra_id,
            'sutra_edition_id': res.id,
            'name': res.name,
            'cover_url': cover_url,
            'finished': True if res.id in finisheds else False,
            'starred': True if res.id in starreds else False})
    return data


def sutras_list(offset, limit):
    result = models.Sutra.objects.filter(
        status=SutraStatus.normal)[offset: offset + limit]
    data = []
    for res in result:
        data.append({
            'id': res.id,
            'name': res.name,
            'cover_url': '{0}{1}'.format(
                settings.SELF_DOMAIN, res.cover_url)})
    return data


def sutras_detail(sutra_id):
    result = models.Sutra.objects.filter(id=sutra_id).first()
    if not result:
        return {}
    return {
        'name': result.name,
        'cover_url': '{0}{1}'.format(
            settings.SELF_DOMAIN, result.cover_url),
        'desc': result.description}


def sutras_edition_list(sutra_id, offset, limit):
    result = models.SutraEdition.objects.filter(
        sutra_id=sutra_id,
        status=SutraEditionStatus.normal)[offset: offset + limit]
    if not result:
        return []
    data = []
    for res in result:
        cover_url = "{0}{1}".format(
            settings.SELF_DOMAIN, res.cover_url)
        data.append({
            'id': res.id,
            'name': res.name,
            'cover_url': cover_url})
    return data


def sutras_edition_detail(edition_id):
    result = models.SutraEdition.objects.filter(
        id=edition_id).first()
    if not result:
        return {}
    return {
        'name': result.name,
        'cover_url': '{0}{1}'.format(
            settings.SELF_DOMAIN, result.cover_url),
        'desc': result.description,
        'price': result.price,
        'author': result.author}


def sutras_edition_content(edition_id, page_num):
    result = models.SutraEditionPage.objects.filter(
        sutra_edition_id=edition_id,
        page_num=page_num).first()
    if not result:
        return {}
    return {
        'content': result.content,
        'original_cimg_url': '{0}{1}'.format(
            settings.SELF_DOMAIN, result.original_cimg_url),
        'hyaline_cimg_url': '{0}{1}'.format(
            settings.SELF_DOMAIN, result.hyaline_cimg_url)}


def get_search_hotwords():
    hotwords = models.HotwordConfig.objects.all().order_by(
        "index").values_list("content", flat=True)
    return list(hotwords)
